﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DWH.Utils;
using System.IO;
using System.Drawing;
using log4net;
using DWH.Data;

namespace DWH.PP.LoopUrls.MMA
{
    public class MMASavePageAttachmentTask : PPLoopUrlsBoardPullTask
    {
        private ILog logger;

        public MMASavePageAttachmentTask()
        {
            logger = LogManager.GetLogger(typeof(MMASavePageAttachmentTask));
        }

        public override string ProcessTargetHtmlContent(string pageContentUrl, string targetHtmlContent)
        {
            if (!String.IsNullOrEmpty(attachmentStoreFolder))
            {
                string attFolderForThisPage = Path.Combine(attachmentStoreFolder, pageContentUrl.GetHashCode().ToString());
                //if (!Directory.Exists(attFolderForThisPage))
                //{
                //    Directory.CreateDirectory(attFolderForThisPage);
                //}
                //move download attachment to another task, skip the foder creation

                //List<string> torrentCans = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent, DWHConstants.REGEX_HTML_A, DWHConstants.REGEX_HTML_A_GROUP_LINKURL);
                
                List<string> images = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent,
                    DWHConstants.REGEX_HTML_IMG, DWHConstants.REGEX_HTML_IMG_GROUP_IMGURL);

                DWHDBHelper.AddUndownloadedPageAttachments(images, pageContentUrl.GetHashCode(),
                    DWHConstants.ATTACHMENT_TYPE_IMAGE,        
                    //"Image",
                    attFolderForThisPage, DateTime.Now);

                //DWHHelper.DownloadWebPageImages(TaskName, attFolderForThisPage, images);               
            }

            return targetHtmlContent;
        }
    }
}
